<template>
  <div class="register">
    <!--卡片视图-->
    <el-card class="register_card">
      <!--头部-->
      <template #header>
        <div class="card-header" style="text-align: left">
          <span style="color: blue">商家注册</span>
        </div>
      </template>
      <!--表单-->
      <el-form
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        size="mini"
        status-icon
        ref="ruleForm"
      >
        <!-- 用户名 -->
        <el-form-item prop="mobile" label="用户名">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item label="密码" prop="encodedPassword">
          <el-input
            type="password"
            v-model="ruleForm.encodedPassword"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="oncePassword">
          <el-input type="password" v-model="ruleForm.oncePassword"></el-input>
        </el-form-item>
        <!--邮箱-->
        <el-form-item prop="attributeValue0" label="邮箱">
          <el-input type="email" v-model="ruleForm.attributeValue0"> </el-input>
        </el-form-item>
        <!--联系方式-->
        <el-form-item prop="attributeValue1" label="手机">
          <el-input type="mobile" v-model="ruleForm.attributeValue1">
          </el-input>
        </el-form-item>
        <!--名称-->
        <el-form-item prop="attributeValue2" label="名称">
          <el-input type="name" v-model="ruleForm.attributeValue2"> </el-input>
        </el-form-item>
        <!--营业执照-->
        <el-form-item prop="attributeValue3" label="营业执照">
          <el-input type="cardId" v-model="ruleForm.attributeValue3">
          </el-input>
          <!-- 地区 -->
        </el-form-item>
        <el-form-item prop="attributeValue4" label="地区">
          <el-input type="cardId" v-model="ruleForm.attributeValue4">
          </el-input>
        </el-form-item>
        <!--验证码-->
        <el-form-item prop="password" label="验证码">
          <el-input
            prefix-icon="el-icon-picture-outline"
            v-model="ruleForm.captcha"
          ></el-input>
          <sidentify :identifyCode="ver"></sidentify>
          <el-link :underline="false" @click.prevent="refresh">刷新验证码</el-link>
        </el-form-item>
      </el-form>
      <!--尾部-->
      <div class="card-footer">
        <el-divider></el-divider>
        <el-checkbox>我已阅读并同意《注册协议》</el-checkbox><br />
        <el-button style="width: 300px" @click="onregister('ruleForm', $event)"
          >注册</el-button
        >
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref } from "vue";
import { verification } from "@/api/verification.js";
import { merchant } from "@/api/register.js";
import Sidentify from "../../components/Sidentify.vue";
export default {
  setup() {
    let ver = ref("");
    function veri() {
      verification().then((res) => {
        ver.value = res.data.data.captcha;
      });
    }
    veri();
    function refresh() {
      veri();
    }
    return {
      ver,
      refresh,
    };
  },
  components: { Sidentify },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.oncePassword !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.encodedPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 登录验证规则
      ruleForm: {
        mobile: "",
        encodedPassword: "",
        oncePassword: "",
        attributeValue0: "",
        attributeValue1: "",
        attributeValue2: "",
        attributeValue3: "",
        attributeValue4: "",
        captcha: "",
      },
      rules: {
        mobile: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        encodedPassword: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        oncePassword: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        attributeValue0: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
        ],
        attributeValue1: [
          { required: true, message: "请输入联系方式", trigger: "blur" },
        ],
        attributeValue2: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    onregister(formName, e) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          merchant(this.ruleForm)
            .then((res) => {
              console.log(res);
              if (res.data.code === 401) {
                this.$message.error(res.data.msg);
                let target = e.target;
                if (target.nodeName == "SPAN") {
                  target = e.target.parentNode;
                }
                target.blur();
                return;
              }
              this.$message({
                message: "注册成功",
                type: "success",
              });
              setTimeout(() => {
                this.$router.push(
                  {
                    path: "/login",
                  },
                  2000
                );
              });
            })
            .catch((err) => {
              console.log(err);
              this.$message.error("注册失败");
            });
        } else {
          let target = e.target;
          if (target.nodeName == "SPAN") {
            target = e.target.parentNode;
          }
          target.blur();
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.register {
  /*背景*/
  /*background: url("../src/assets/backgroud.jepg");*/
  width: 100%;
  padding-top: 50px;
  padding-bottom: 50px;
  margin: 0 auto;
}
.register_card {
  width: 900px;
  margin: 0 auto;
}
.card-footer {
  text-align: center;
}
.register_form {
  width: 800px;
  margin: 0 auto;
}
</style>
